package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    WorkspaceService workspaceService;
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList=new ArrayList();
        List<Double> turnoverList=new ArrayList();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate o : dateList) {
            LocalDateTime begins = LocalDateTime.of(o, LocalTime.MIN);
            LocalDateTime ends = LocalDateTime.of(o, LocalTime.MAX);
            Map map=new HashMap<>();
            map.put("begin", begins);
            map.put("end", ends);
            map.put("status", Orders.COMPLETED);
            Double s=orderMapper.getSumAmount(map);
            turnoverList.add(s);
        }

        return TurnoverReportVO.builder().dateList(StringUtils.join(dateList,",")).turnoverList(StringUtils.join(turnoverList,",")).build();
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList=new ArrayList();
        List newUserList=new ArrayList();
        List totalUserList=new ArrayList();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate o : dateList) {
            LocalDateTime begins = LocalDateTime.of(o, LocalTime.MIN);
            LocalDateTime ends = LocalDateTime.of(o, LocalTime.MAX);
            Map map=new HashMap<>();
            map.put("end", ends);
            int sum=orderMapper.getSumUser(map);
            map.put("begin", begins);
            int count=orderMapper.getSumUser(map);
            newUserList.add(count);
            totalUserList.add(sum);
        }
        return UserReportVO.builder().dateList(StringUtils.join(dateList,",")).newUserList(StringUtils.join(newUserList,",")).totalUserList(StringUtils.join(totalUserList,",")).build();
    }

    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList=new ArrayList();
        List orderCountList=new ArrayList();//订单数列表
        List validOrderCountList=new ArrayList();//有效订单数列表
        Double orderCompletionRate=0.00;//订单完成率
        int totalOrderCount=0;//订单总数
        int validOrderCount=0;//有效订单总数
        LocalDate newbegin=begin;
        dateList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate localDate : dateList) {
            LocalDateTime begins = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime ends = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map=new HashMap<>();
            map.put("begin", begins);
            map.put("end", ends);
            int orderCount= orderMapper.getReportOrders(map);
            map.put("status", Orders.COMPLETED);
            int validOrderCounts= orderMapper.getReportOrders(map);
            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCounts);
        }
        totalOrderCount=orderMapper.gettotalOrderCount(LocalDateTime.of(newbegin, LocalTime.MIN),LocalDateTime.of(end, LocalTime.MAX));
        validOrderCount=orderMapper.getvalidOrderCount(LocalDateTime.of(newbegin, LocalTime.MIN),LocalDateTime.of(end, LocalTime.MAX),Orders.COMPLETED);
        orderCompletionRate=validOrderCount*1.0/totalOrderCount;
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .orderCompletionRate(orderCompletionRate)
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        LocalDateTime begins=LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime ends=LocalDateTime.of(end, LocalTime.MAX);
        List<String> nameList=new ArrayList();
        List<Long> numberList=new ArrayList();
        List<Long> orderIdList = orderMapper.getOrderIdList(begins, ends,Orders.COMPLETED);
        nameList=orderMapper.getTop10(orderIdList);
        numberList=orderMapper.getTop10Number(orderIdList);
        return SalesTop10ReportVO.builder().nameList(StringUtils.join(nameList,",")).numberList(StringUtils.join(numberList,",")).build();
    }


    @Override
    public void export(HttpServletResponse response) {
        LocalDate begin = LocalDate.now().plusDays(-30);
        LocalDate end = LocalDate.now().plusDays(-1);
        BusinessDataVO businessDataVO = workspaceService.businessData(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(in);
            XSSFSheet sheet1 = excel.getSheet("Sheet1");
            sheet1.getRow(1).getCell(1).setCellValue("表格日期为："+begin+"至"+end);
            sheet1.getRow(3).getCell(2).setCellValue(businessDataVO.getTurnover()+"元");
            sheet1.getRow(3).getCell(4).setCellValue(businessDataVO.getOrderCompletionRate()*100+"%");
            sheet1.getRow(3).getCell(6).setCellValue(businessDataVO.getNewUsers()+"人");
            sheet1.getRow(4).getCell(2).setCellValue(businessDataVO.getValidOrderCount()+"单");
            sheet1.getRow(4).getCell(4).setCellValue(businessDataVO.getUnitPrice()+"元");
            for (int i = 0; i <30; i++) {
                LocalDate localDate = begin.plusDays(i);
                BusinessDataVO businessDataVOs = workspaceService.businessData(LocalDateTime.of(localDate, LocalTime.MIN), LocalDateTime.of(localDate, LocalTime.MAX));
                sheet1.getRow(i + 7).getCell(1).setCellValue(localDate + "");
                sheet1.getRow(i + 7).getCell(2).setCellValue(businessDataVOs.getTurnover() + "元");
                sheet1.getRow(i + 7).getCell(3).setCellValue(businessDataVOs.getValidOrderCount() + "单");
                sheet1.getRow(i + 7).getCell(4).setCellValue(businessDataVOs.getOrderCompletionRate() * 100 + "%");
                sheet1.getRow(i + 7).getCell(5).setCellValue(businessDataVOs.getUnitPrice() + "元");
                sheet1.getRow(i + 7).getCell(6).setCellValue(businessDataVOs.getNewUsers() + "个");
            }
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);
            outputStream.close();
            excel.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

//
//
//


    }
}






